package com.eshop.logging.rest;

import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import com.eshop.utils.SecurityUtils;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.data.domain.Pageable;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import io.swagger.annotations.ApiOperation;
import com.eshop.logging.aop.log.Log;
import java.io.IOException;
import com.eshop.logging.service.dto.LogQueryCriteria;
import javax.servlet.http.HttpServletResponse;
import com.eshop.logging.service.LogService;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping({ "/api/logs" })
@Api(tags = { "监控：日志管理" })
public class LogController
{
    private final LogService logService;
    
    public LogController(final LogService logService) {
        this.logService = logService;
    }
    
    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping({ "/download" })
    @PreAuthorize("@el.check('admin','log:list')")
    public void download(final HttpServletResponse response, final LogQueryCriteria criteria) throws IOException {
        criteria.setLogType("INFO");
        this.logService.download(this.logService.queryAll(criteria), response);
    }
    
    @Log("导出错误数据")
    @ApiOperation("导出错误数据")
    @GetMapping({ "/error/download" })
    @PreAuthorize("@el.check('admin','log:list')")
    public void errorDownload(final HttpServletResponse response, final LogQueryCriteria criteria) throws IOException {
        criteria.setLogType("ERROR");
        this.logService.download(this.logService.queryAll(criteria), response);
    }
    
    @GetMapping
    @ApiOperation("日志查询")
    @PreAuthorize("@el.check('admin','log:list')")
    public ResponseEntity<Object> getLogs(final LogQueryCriteria criteria, final Pageable pageable) {
        criteria.setLogType("INFO");
        criteria.setType(0);
        return (ResponseEntity<Object>)new ResponseEntity(this.logService.queryAll(criteria, pageable), HttpStatus.OK);
    }
    
    @GetMapping({ "/mlogs" })
    @PreAuthorize("@el.check('admin','log:list')")
    public ResponseEntity getApiLogs(final LogQueryCriteria criteria, final Pageable pageable) {
        criteria.setLogType("INFO");
        criteria.setType(1);
        return new ResponseEntity(this.logService.findAllByPageable(criteria.getBlurry(), pageable), HttpStatus.OK);
    }
    
    @GetMapping({ "/user" })
    @ApiOperation("用户日志查询")
    public ResponseEntity<Object> getUserLogs(final LogQueryCriteria criteria, final Pageable pageable) {
        criteria.setLogType("INFO");
        criteria.setBlurry(SecurityUtils.getUsername());
        return (ResponseEntity<Object>)new ResponseEntity(this.logService.queryAllByUser(criteria, pageable), HttpStatus.OK);
    }
    
    @GetMapping({ "/error" })
    @ApiOperation("错误日志查询")
    @PreAuthorize("@el.check('admin','logError:list')")
    public ResponseEntity<Object> getErrorLogs(final LogQueryCriteria criteria, final Pageable pageable) {
        criteria.setLogType("ERROR");
        return new ResponseEntity(this.logService.queryAll(criteria, pageable), HttpStatus.OK);
    }
    
    @GetMapping({ "/error/{id}" })
    @ApiOperation("日志异常详情查询")
    @PreAuthorize("@el.check('admin','logError:detail')")
    public ResponseEntity<Object> getErrorLogs(@PathVariable final Long id) {
        return (ResponseEntity<Object>)new ResponseEntity(this.logService.findByErrDetail(id), HttpStatus.OK);
    }
    
    @DeleteMapping({ "/del/error" })
    @Log("删除所有ERROR日志")
    @ApiOperation("删除所有ERROR日志")
    @PreAuthorize("@el.check('admin','logError:remove')")
    public ResponseEntity<Object> delAllByError() {
        this.logService.delAllByError();
        return (ResponseEntity<Object>)new ResponseEntity(HttpStatus.OK);
    }
    
    @DeleteMapping({ "/del/info" })
    @Log("删除所有INFO日志")
    @ApiOperation("删除所有INFO日志")
    @PreAuthorize("@el.check('admin','logInfo:remove')")
    public ResponseEntity<Object> delAllByInfo() {
        this.logService.delAllByInfo();
        return (ResponseEntity<Object>)new ResponseEntity(HttpStatus.OK);
    }
}
